package com.houtai.controller.company;

import com.aliyun.openservices.oss.model.ObjectMetadata;
import com.houtai.authority.Authority;
import com.houtai.authority.AuthorityType;
import com.houtai.base.ResultVo;
import com.houtai.bean.dto.avtivity.works.TdCompanyIndexWorksDto;
import com.houtai.bean.dto.company.TdCompanyIndexDto;
import com.houtai.bean.dto.company.TdCompanyInsertDto;
import com.houtai.bean.dto.company.TdCompanyUpdateDto;
import com.houtai.bean.vo.activity.works.TdActivityWorksListVo;
import com.houtai.bean.vo.company.TdCompanyDetailVo;
import com.houtai.bean.vo.company.TdCompanyIndexVo;
import com.houtai.bean.vo.file.FileVo;
import com.houtai.common.Contant;
import com.houtai.common.oss.OssUtil;
import com.houtai.common.util.CommonUtil;
import com.houtai.service.company.CompanyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@Api(value="企业信息接口", tags={"企业信息接口"})
@RequestMapping(value = {"api/v1"}, method = RequestMethod.POST)
public class CompanyContorller {
    @Autowired
    private CompanyService companyService;



    @ApiOperation(value="发起认证", notes="发起认证")
    @Authority(AuthorityType.Company)
    @RequestMapping(value = "/company/info")
    public ResultVo list(@ApiParam(value = "发起认证所需参数") @Valid @RequestBody TdCompanyInsertDto dto, BindingResult result,
                         @RequestHeader String token) {
        ResultVo retInfo = new ResultVo();
        if (result.hasErrors()) {
            List<FieldError> err = result.getFieldErrors();
            FieldError fe = err.get(0);
            retInfo.setMark("1");
            retInfo.setTip(fe.getDefaultMessage());
        } else {
            retInfo = companyService.insertTdCompany(dto, token);
        }
        return retInfo;
    }

    @ApiOperation(value="企业资料", notes="企业资料")
    @Authority(AuthorityType.Company)
    @RequestMapping(value = "/company/info/detail")
    public TdCompanyDetailVo list(@RequestHeader String token) {

        return companyService.userInfoDetail(token);
    }


    @ApiOperation(value="修改企业资料", notes="修改企业资料")
    @Authority(AuthorityType.CompanyReal)
    @RequestMapping(value = "/company/info/update")
    public ResultVo update(@ApiParam(value = "修改企业资料所需参数") @Valid @RequestBody TdCompanyUpdateDto dto, BindingResult result,
                           @RequestHeader String token) {
        ResultVo retInfo = new ResultVo();
        if (result.hasErrors()) {
            List<FieldError> err = result.getFieldErrors();
            FieldError fe = err.get(0);
            retInfo.setMark("1");
            retInfo.setTip(fe.getDefaultMessage());
        } else {
            retInfo = companyService.updateTdCompany(dto, token);
        }
        return retInfo;
    }
    @Authority(value = AuthorityType.Login)
    @ApiOperation(value="企业logo 企业营业执照上传(次接口为表单类型)", notes="企业logo 企业营业执照上传")
    @RequestMapping(value = "/company/file", method = RequestMethod.POST)
    public FileVo file(@RequestParam(value = "frontFile", required = true) MultipartFile frontFile)throws Exception{
        FileVo vo = new FileVo();
        if (CommonUtil.checkFileSize(frontFile.getSize(), 100,"K")) {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddhhmmssS", Locale.CHINA);

            String frontFileName = frontFile.getOriginalFilename().substring(frontFile.getOriginalFilename().lastIndexOf(".") + 1);
            String imgName = formatter.format(new Date(System.currentTimeMillis()));
            String frontSavePath = Contant.FILE_IMG + "/company/" + imgName + "." + frontFileName;
            ObjectMetadata frontMetadata = new ObjectMetadata();

            InputStream inputStream = frontFile.getInputStream();
            frontMetadata.setContentLength(inputStream.available());
            OssUtil.uploadInputStream(inputStream, frontMetadata, frontSavePath);
            vo.setMark("0");
            vo.setTip("查询成功");
            vo.setFile_name(imgName + "." + frontFileName);
            vo.setFile_url(Contant.DOMAIN +  Contant.FILE_IMG + "/company/" + imgName + "." + frontFileName);
        } else {
            vo.setMark("1");
            vo.setTip("作品文件最大100K");
        }

        return vo;
    }
    @ApiOperation(value="企业主页", notes="企业主页")
    @Authority(AuthorityType.NoValidate)
    @RequestMapping(value = "/company/index")
    public TdCompanyIndexVo update(@ApiParam(value = "企业主页所需参数") @Valid @RequestBody TdCompanyIndexDto dto, BindingResult result,
                                   @RequestHeader String token) {
        TdCompanyIndexVo retInfo = new TdCompanyIndexVo();
        if (result.hasErrors()) {
            List<FieldError> err = result.getFieldErrors();
            FieldError fe = err.get(0);
            retInfo.setMark("1");
            retInfo.setTip(fe.getDefaultMessage());
        } else {
            retInfo = companyService.companyIndex(dto.getCompany_id());
        }
        return retInfo;
    }
    @ApiOperation(value="企业主页作品列表", notes="企业主页作品列表")
    @Authority(AuthorityType.NoValidate)
    @RequestMapping(value = "/company/works/list")
    public TdActivityWorksListVo works(@ApiParam(value = "企业主页作品列表所需参数") @Valid @RequestBody TdCompanyIndexWorksDto dto, BindingResult result,
                                       @RequestHeader String token) {
        TdActivityWorksListVo retInfo = new TdActivityWorksListVo();
        if (result.hasErrors()) {
            List<FieldError> err = result.getFieldErrors();
            FieldError fe = err.get(0);
            retInfo.setMark("1");
            retInfo.setTip(fe.getDefaultMessage());
        } else {
            retInfo = companyService.companyWorksList(dto);
        }
        return retInfo;
    }
}
